import {service} from '@/utils/request'

// 登录接口
export interface LoginParams {
  username: string
  password: string
}

export interface LoginResponse {
  access_token: string
  refresh_token: string
  user: {
    id: number
    username: string
    nickname: string
    email: string
    avatar: string
    roles: string[]
    permissions: string[]
  }
}

/**
 * 用户登录
 */
export const login = (data: LoginParams) => {
  return service<LoginResponse>({
    url: '/auth/login/password',
    method: 'post',
    data
  })
}

/**
 * 获取用户信息
 */
export const getUserInfo = () => {
  return service({
    url: '/auth/profile',
    method: 'get'
  })
}

/**
 * 用户登出
 */
export const logout = () => {
  return service({
    url: '/auth/logout',
    method: 'post'
  })
}

/**
 * 刷新Token
 */
export const refreshToken = (refreshToken: string) => {
  return service({
    url: '/auth/refresh',
    method: 'post',
    data: { refresh_token: refreshToken }
  })
}